package com.zr.paymentInformation.controller;

import com.zr.paymentInformation.meiju.huankuanDropdownBox;
import com.zr.paymentInformation.meiju.kechengDropdownBox;
import com.zr.paymentInformation.pojo.PaymentInformationAddVo;
import com.zr.paymentInformation.pojo.XiaLaVo;
import com.zr.paymentInformation.service.PaymentInformationService;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/7/3 0003.
 */
@RestController
public class PaymentInformationController {
    @Autowired
    private PaymentInformationService paymentInformationService;
    /**
     * 分期缴费申请查询
     *
     */
    @GetMapping("queryPaymentById")
    public ResultVo queryPayById(HttpServletRequest request){
        Integer id= (Integer) request.getSession().getAttribute("loginId");

        return paymentInformationService.queryById(id);
    }

    /**
     *
     * 分期缴费申请信息添加接口-
     */

    @PostMapping("installmentApply")
    public ResultVo installmentApply(@RequestBody @Valid PaymentInformationAddVo paymentInformationAddVo,BindingResult bindingResult,HttpServletRequest request){

        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(), "500");
        }
        return paymentInformationService.installmentApply(paymentInformationAddVo,request);
    }

    /**
     *
     * 课程下拉框接口-枚举-
     */
    @GetMapping("kechengDropdownBox")
    public ResultVo<List<XiaLaVo>> kechengDropdownBox(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< kechengDropdownBox.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setId(i);
            xiaLaVo.setName(kechengDropdownBox.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultVOBuilder.success(xiaLaVoList);
    }


    /**
     *
     * 还款周期下拉框接口--枚举
     */
    @GetMapping("huankuanDropdownBox")
    public ResultVo<List<XiaLaVo>> huankuanDropdownBox(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< huankuanDropdownBox.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setId(i);
            xiaLaVo.setName(huankuanDropdownBox.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultVOBuilder.success(xiaLaVoList);
    }
}
